package com.sugar.follow.up.module.business.controller.admin.followuprecord;

import com.sugar.follow.up.framework.apilog.core.annotation.ApiAccessLog;
import com.sugar.follow.up.framework.common.pojo.CommonResult;
import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.security.core.util.SecurityFrameworkUtils;
import com.sugar.follow.up.module.business.controller.admin.followuprecord.vo.FollowUpRecordDetailRespVO;
import com.sugar.follow.up.module.business.controller.admin.followuprecord.vo.FollowUpRecordPageReqVO;
import com.sugar.follow.up.module.business.controller.admin.followuprecord.vo.FollowUpRecordRespVO;
import com.sugar.follow.up.module.business.controller.admin.followuprecord.vo.FollowUpRecordSaveReqVO;
import com.sugar.follow.up.module.business.service.followuprecord.FollowUpRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

import static com.sugar.follow.up.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.sugar.follow.up.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.follow.up.framework.common.pojo.CommonResult.success;
import static com.sugar.follow.up.module.business.enums.ErrorCodeConstants.FOLLOW_UP_RECORD_NO_SELECT_PATIENT;
import static com.sugar.follow.up.module.business.enums.ErrorCodeConstants.USER_PATIENT_TYEP_NOT_EMPTY;

@Tag(name = "管理后台 - 随访发起记录")
@RestController
@RequestMapping("/business/follow-up-record")
@Validated
public class FollowUpRecordController {

    @Resource
    private FollowUpRecordService followUpRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建随访发起记录")
    @PreAuthorize("@ss.hasPermission('business:follow-up-record:create')")
    public CommonResult<Boolean> createFollowUpRecord(@Valid @RequestBody FollowUpRecordSaveReqVO createReqVO) {
        if (Objects.isNull(createReqVO.getType()))
            throw exception(USER_PATIENT_TYEP_NOT_EMPTY);
        if (!createReqVO.getType()){
            Long[] userIds = createReqVO.getUserIds();
            if (userIds == null || userIds.length == 0)
                throw exception(FOLLOW_UP_RECORD_NO_SELECT_PATIENT);
        }else {
            List<String[]> tags = createReqVO.getTags();
            if (tags == null || tags.isEmpty())
                throw exception(FOLLOW_UP_RECORD_NO_SELECT_PATIENT);
        }

        followUpRecordService.createFollowUpRecord(createReqVO, SecurityFrameworkUtils.getLoginUserId());
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新随访发起记录")
    @PreAuthorize("@ss.hasPermission('business:follow-up-record:update')")
    public CommonResult<Boolean> updateFollowUpRecord(@Valid @RequestBody FollowUpRecordSaveReqVO updateReqVO) {
        Long[] userIds = updateReqVO.getUserIds();
        if (userIds == null || userIds.length != 2)
            throw exception(FOLLOW_UP_RECORD_NO_SELECT_PATIENT);

        followUpRecordService.updateFollowUpRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除随访发起记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:follow-up-record:delete')")
    public CommonResult<Boolean> deleteFollowUpRecord(@RequestParam("id") Long id) {
        followUpRecordService.deleteFollowUpRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除随访发起记录")
                @PreAuthorize("@ss.hasPermission('business:follow-up-record:delete')")
    public CommonResult<Boolean> deleteFollowUpRecordList(@RequestParam("ids") List<Long> ids) {
        followUpRecordService.deleteFollowUpRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得随访发起记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:follow-up-record:query')")
    public CommonResult<FollowUpRecordDetailRespVO> getFollowUpRecord(@RequestParam("id") Long id) {
        return success(followUpRecordService.getFollowUpRecord(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得随访发起记录分页")
    @PreAuthorize("@ss.hasPermission('business:follow-up-record:query')")
    public CommonResult<PageResult<FollowUpRecordRespVO>> getFollowUpRecordPage(@Valid FollowUpRecordPageReqVO pageReqVO) {
        return success(followUpRecordService.getFollowUpRecordPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出随访发起记录 Excel")
    @PreAuthorize("@ss.hasPermission('business:follow-up-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFollowUpRecordExcel(@Valid FollowUpRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        followUpRecordService.exportFollowUpRecordExcel(pageReqVO, response);
    }

}